package com.yzy.tool
{
	import com.yzy.models.SetLibs;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.media.Sound;
	import flash.system.ImageDecodingPolicy;
	import flash.system.LoaderContext;
	import flash.utils.ByteArray;
	import flash.utils.Endian;

	public class ByteResolve extends EventDispatcher
	{
		public function ByteResolve()
		{
		}
		
		/**
		 * byteArray To Bitmap :异步加载 
		 * @param byte：ByteArray
		 * @param bitmap： Bitmap
		 * 
		 */		
		public function byteToBitmap(byte:ByteArray,bitmap:Bitmap):void{
			var _loader:Loader=new Loader();
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function LoadComplete(evt:Event):void{
				var _imageData:BitmapData=new BitmapData(_loader.width,_loader.height);
				_imageData.draw(_loader,null,null,null,null,true);
				bitmap.bitmapData=_imageData;
			});
			var loaderContext:LoaderContext = new LoaderContext(); 
			loaderContext.imageDecodingPolicy = ImageDecodingPolicy.ON_LOAD;
		    _loader.loadBytes(byte,loaderContext); 
		}
		
		/**
		 * 解析swf 存入 SetLibs 
		 * @param byte
		 * 
		 */		
		public static function byteForSwf(bytes:ByteArray,name:String,type:String,completeFun:Function):void{
			var loader:Loader=new Loader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function loaderSwfHandle(evt:Event):void{
				loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,loaderSwfHandle);
				var loaderinfo:LoaderInfo=evt.currentTarget as LoaderInfo;
				switch(type)
				{
					case "swf":
					{
						
						var ElementNames:Vector.<String>=SwfMcNameArr(bytes);
						var enl:int=ElementNames.length;
						for(var i:int=0;i<enl;i++){
							/** set swf class */
							var ClassName:Class=loaderinfo.applicationDomain.getDefinition(ElementNames[i]) as Class;
							if(new ClassName() as BitmapData){
								SetLibs.setBitmapData(ElementNames[i],new ClassName());
							}else if(new ClassName() as Sound){
								SetLibs.setSoundDs(ElementNames[i],new ClassName());
							}else{
								SetLibs.setMovieClipDs(ElementNames[i],new ClassName());
							}
						}
						break;
					}
					case "jpg":	
					case "png":
					{
						var bitmap:Bitmap=loaderinfo.content as Bitmap;
						SetLibs.setBitmapData(name,bitmap.bitmapData);					
						break;
					}
					case "sound":
					{
						var sound:Sound=loaderinfo.content as Sound;
						SetLibs.setSoundDs(ElementNames[i],sound);
						break;
					}	
					default:
					{
						
						break;
					}
				}
				loader.unload();
				completeFun();
			});
			var loaderContext:LoaderContext=new LoaderContext();
			loaderContext.imageDecodingPolicy=ImageDecodingPolicy.ON_LOAD;
			loaderContext.allowCodeImport=true;
			loader.loadBytes(bytes,loaderContext);
		}
		
		
		//**********************************************
		/**
		 *  
		 * @param bytes
		 * @return 
		 * 
		 */		
		public static function SwfMcNameArr(bytes:ByteArray):Vector.<String>{
			var mcNameArr:Vector.<String>=new Vector.<String>();		
			//
			bytes.endian=Endian.LITTLE_ENDIAN;
			bytes.writeBytes(bytes,8);
			bytes.uncompress();
			bytes.position=Math.ceil(((bytes[0]>>>3)*4+5)/8)+4;
			while(bytes.bytesAvailable>2){
				var head:int=bytes.readUnsignedShort();
				var size:int=head&63;
				if (size==63){
					size=bytes.readInt();
				}
				if (head>>6!=76){
					bytes.position+=size;
				}else {
					head=bytes.readShort();
					for(var i:int=0;i<head;i++){
						bytes.readShort();
						size=bytes.position;
						while(bytes.readByte()!=0){};
						size=bytes.position-(bytes.position=size);
						mcNameArr.push(bytes.readUTFBytes(size));
					}
				}
			}
			return mcNameArr;
		}
		//*****************************************************
		// Singleton 
		//*****************************************************
		private static var _instant:ByteResolve;
		public static function get instant():ByteResolve
		{
			if(null == _instant)
			{
				_instant =  new ByteResolve();
			}
			return _instant;
		}
	}
}